<?php
ob_start();
?>
<?php
include ('encabezado.php');
?>
<form action="" method="POST">
<br><br>
<table align="center" border="2">
<tr>
<td align="center">
<h2>Create marks <br></h2>
Type: <br>
<select name="type">
<option value="exam">Exam</option>
<option value="xp">XP</option>
</select><br><br>
Mark: <br>
<input type="text" name="nota" /><br><br>
Description: <br>
<input type="text" name="description" /><br><br>
Date: <br>
<input type="text" readonly='readonly' name="date" id="campofecha"/><br><br>

Applies to: <br>
<?php
$conexion = mysql_connect("$ip", "$userdb", "$passworddb");
	mysql_select_db("$db", $conexion);	
	$_SESSION['choosen'] = $choosengroup;	
	$q = "SELECT $db.Users.Name, $db.Users.Surname, $db.Groups.GroupID
	FROM Users
	INNER JOIN Groups ON $db.Users.GroupID = $db.Groups.GroupID where $db.Groups.Name = '$choosengroup'";		
	$losgrupos= mysql_query($q, $conexion) or die ("problema con query");
	print "<select name=\"solucion\">\"<option value=\"todos\" name=\"todos\">All students</option>\"";
	while ($registro = mysql_fetch_array($losgrupos))
	{
		$nombrecomleto = $registro['Surname'].','.$registro['Name'];
		
		print "<option value=\"{$nombrecomleto}\" name=\"{$registro['GroupID']}\">{$nombrecomleto}</option>";
	}
	print "</select><br><br>";
	
?>
<?php



if(isset($_POST['enviado'])){

$type = $_POST['type'];
$des = $_POST['description'];
$date = $_POST['date'];
$mark = $_POST['nota'];
$solucion = $_POST['solucion'];

		if($solucion == 'todos'){
		$q = "SELECT $db.Users.Name, $db.Users.UserID, $db.Users.Surname, $db.Groups.GroupID
		FROM Users
		INNER JOIN Groups ON $db.Users.GroupID = $db.Groups.GroupID where $db.Groups.Name = '$choosengroup'";		
		$losgrupos2= mysql_query($q, $conexion) or die ("problema con query");
		
			while ($registro2 = mysql_fetch_array($losgrupos2))
			{
				$userid = $registro2['UserID'];
				$q2 = "INSERT INTO Marks (UserID, Mark, Type, Description, Date)
					   VALUES ('$userid','$mark','$type','$des','$date')";
				$insert = mysql_query($q2, $conexion) or die ("problema con query");
				
				
			}
		
		}
		
		else {
		
				$nombrecomp = $_POST['solucion'];
				$var = explode(',', $nombrecomp);
				$nombre = $var[1];		
				$apellido = $var[0];
				$q3 = "SELECT UserID FROM Users where Name='$nombre' and Surname='$apellido'";	
				//echo $q3;
				$id = mysql_query($q3) or die ("problema con query");
				$registro3 = mysql_fetch_array($id);
				$id = $registro3[0];
					$q4 = "INSERT INTO Marks (UserID, Mark, Type, Description, Date)
						   VALUES ('$id','$mark','$type','$des','$date')";
					$insert = mysql_query($q4, $conexion) or die ("problema con query");
		
		}
}
?>
<input type="submit" value="Create Mark">
<input type="hidden" name="enviado" value="true">
</td></tr>
</table>
</form>
<?php
include ('pie.php');
?>
<?php
ob_end_flush();
?>